home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 1.iso / games / wordy313.zip / INSET.C < prev    next >
C/C++ Source or Header  |  1995-04-30  |  6KB  |  208 lines

  1. /**************************************************************************/
  2. /*                             Inset   Utility                        */
  3. /*                                                            */
  4. /*                                 M\Cooper                            */
  5. /*                        3425 Chestnut Ridge Rd.                        */
  6. /*                        Grantsville, MD 21536-9801                    */
  7. /*                        --------------------------                    */
  8. /*                        Email:  thegrendel@aol.com                    */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package             */
  11. /*                                                            */    
  12. /**************************************************************************/
  13.  
  14.  
  15. /**********************************WORDTEST********************************/
  16. /*       Function tests if word is constructible from Letterset            */
  17. /*                 Args in: char *letterset, char *word                          */
  18. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  19. /**************************************************************************/
  20.  
  21. #include <conio.h>
  22. #include "srch.h"
  23.  
  24.  
  25. #define FILE_OPENING_ERROR 3
  26. #define FILENAME_MAXLEN 8
  27. #define CR "\n"
  28. #define FILE_SUFFIX ".set"
  29. #define MAXLEN 30
  30. #define LINE_LEN 80
  31. #define NOARGS 1
  32. #define INCREMENT 1
  33. #define SPACE ' '
  34.  
  35. #define BUFFERSIZE 8192
  36.  
  37. char ad[] =
  38. "INSET utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  39.  
  40.  
  41. void getword( char *lset );
  42. void center( char *strng );
  43.  
  44. typedef enum { FALSE, TRUE } Boolean;
  45.  
  46. void main( int argc, char **argv )
  47. {
  48.  
  49.    char letterset[ MAXLEN ];
  50.  
  51.      if( argc == NOARGS )
  52.         {
  53.         clrscr();
  54.         puts("Enter a LETTERSET to test ... ");
  55.         gets( letterset );
  56.         }
  57.      else
  58.         strcpy( letterset, *( argv + 1 ) );
  59.  
  60.      getword( letterset );
  61. }
  62.  
  63.  
  64.  
  65. Boolean wordtest( char *letterset, char *word )
  66. {
  67.     Boolean error_flag;
  68.  
  69.       if( strstr( word, letterset ) )
  70.          error_flag = TRUE;
  71.       else
  72.          error_flag = FALSE;
  73.         return( error_flag );
  74. }
  75.  
  76. /*************************************************************/
  77. void getword( char *letter_set )
  78. {
  79.  
  80.     char    l_set [ MAXLEN ],
  81.         word [ MAXLEN ],
  82.         tempstr [ MAXLEN + 1 ],
  83.         targetfile [ MAXLEN ],
  84.         bar [ LINE_LEN + 1 ],
  85.         double_bar [ LINE_LEN + 1 ];
  86.  
  87.     FILE *fptr,
  88.         *tfile;
  89.     int fnamelen;
  90.     long wcount = 0L;
  91.  
  92.        memset( bar, '-', LINE_LEN );
  93.        *( bar + LINE_LEN ) = NULL;
  94.        memset( double_bar, '=', LINE_LEN );
  95.        *( double_bar + LINE_LEN ) = NULL;
  96.  
  97.        /*************opening credits*************/
  98.        clrscr();
  99.        printf( double_bar );
  100.        strcpy( tempstr, ad );
  101.        center ( tempstr );
  102.        printf( tempstr );
  103.        printf( CR );
  104.        printf( double_bar );
  105.        printf( CR );
  106.        /****************************************/
  107.  
  108.  
  109.        strcpy ( l_set, letter_set );
  110. //       strcat ( letter_set, CR );
  111.  
  112.        /*   Create name of file to store derived words in   */
  113.        /*********************************************************/
  114.        fnamelen = strlen( l_set );
  115.        if( fnamelen  > FILENAME_MAXLEN )
  116.           fnamelen = FILENAME_MAXLEN;
  117.        strncpy( targetfile, l_set, fnamelen );
  118.        *( targetfile + fnamelen ) = NULL;
  119.        //NULL-terminate string, so strcat works, ha, ha.
  120.        strcat( targetfile, FILE_SUFFIX );
  121.        /*********************************************************/
  122.  
  123.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  124.          {
  125.          printf( "\7\7\7Cannot open Wordfile!" );
  126.          exit( FILE_OPENING_ERROR );
  127.          }
  128.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  129.          exit( FILE_OPENING_ERROR + 1 );
  130.  
  131.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  132.          {
  133.          printf( "\7\7\7Cannot open file to save words in!" );
  134.          exit ( FILE_OPENING_ERROR + 2 );
  135.          }
  136.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  137.          exit( FILE_OPENING_ERROR + 3 );
  138.  
  139.        /**************'Wait' Message************/
  140.        printf( CR CR );
  141.        printf( "WORKING...\n\n" );
  142.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  143.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  144.        printf( "Now searching 99,000+ word file \nand writing file of valid words containing -%s-.\n\n", 
  145.             letter_set );
  146.        /*****************************************/
  147.  
  148.  
  149.  
  150.  
  151.  
  152.        sprintf( tempstr, "Words created from: %s\n", strupr( l_set ) );
  153.        center( tempstr );
  154.        fprintf( tfile, double_bar );
  155.       fprintf( tfile, CR );
  156.        fprintf( tfile, tempstr );
  157.        fprintf( tfile, double_bar );
  158.        fprintf( tfile, CR );
  159.  
  160.  
  161.          /*********************Main Loop*************/     
  162.           while( fgets( word, MAXLEN, fptr ) != NULL )
  163.  
  164.             if( wordtest( letter_set, word ) )
  165.                {
  166.                fprintf( tfile, "%s", word );
  167.                wcount++;
  168.                }
  169.           /*******************************************/
  170.  
  171.           fprintf( tfile, bar );
  172.       fprintf( tfile, CR );
  173.           sprintf( tempstr, "%ld words can be constructed from %s.",
  174.                  wcount - INCREMENT, l_set );
  175.           center( tempstr );              
  176.           fprintf( tfile, tempstr );
  177.           fprintf( tfile, "\n\n" );
  178.  
  179.           center( ad );
  180.           fprintf( tfile, ad );
  181.  
  182.           fcloseall();
  183.  
  184.           sprintf( tempstr,
  185.                  "The file %s has %ld words derived from %s\7.",
  186.                  targetfile, wcount - INCREMENT, l_set );
  187.           center( tempstr );
  188.           printf( CR CR );
  189.           printf( tempstr );
  190.  
  191. }
  192.  
  193.  
  194.  
  195. void center( char *str )
  196. {
  197.    int padding;
  198.    char st [ LINE_LEN + INCREMENT ];
  199.  
  200.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  201.      memset( st, SPACE, padding );
  202.      *( st + padding ) = NULL;  //Terminate string
  203.      strcat( st, str );
  204.      strcpy( str, st );
  205.  
  206.      return;
  207. }
  208.